#pragma once

#include <iostream>
#include <string>
#include <cstdio>
#include<set>
#include "Log.hpp"
using namespace LogModule;
class Command
{
public:
    Command() {
        _white_list.insert("ls");
        _white_list.insert("pwd");
        _white_list.insert("whoami");
        _white_list.insert("tree ./");
        _white_list.insert("ls -l");
    }
    bool SafeCheck(std::string& message)
    {
        auto item=_white_list.find(message);
        if(item==_white_list.end())
        return false;
        return true;
    }
    std::string Execute(std::string message)
    {
        LOG(LogLevel::DEBUG)<<"我进入Execute了";
        if(SafeCheck(message)==0)return message+"是非法命令";
        LOG(LogLevel::DEBUG)<<"我是安全的";
        FILE *fd = popen(message.c_str(), "r");
        if (fd == nullptr)
        {
            LOG(LogLevel::WARING) << "popen error";
            return "FAIL";
        }
        LOG(LogLevel::DEBUG)<<"我执行好了的";
        char buff[1024];
        std::string result;
        while (true)
        {
            char *ret = fgets(buff, sizeof buff, fd);
            if (ret == nullptr)
                break;
            result += buff;
        }
        return result.empty() ? "命名为空" : result;
        pclose(fd);
        return message;
    }
    ~Command() {}

private:
std::set<std::string> _white_list;
};
